var express = require('express');
var router = express.Router();
var db = require('../../../libs/mysql');
//统一返回格式
var res_Data;
router.use( function(req, res, next) {
    res_Data = {
        code: 0,
        message: ''
    }
    next();
});

router.get('/',function (req,res) {
    var id = req.query.id || '';
    var cateid = null;

    db.query(`SELECT * FROM category`,(err,navs)=>{
        if(err){
            res.send('前台分类查询').end();
        }else {

            var sql = 'SELECT u.nickname,c.`name`,t.* FROM contents t LEFT JOIN `user` u ON t.userid = u.id LEFT JOIN category c ON t.categoryid = c.id WHERE t.id='+id+'';
            db.query(sql,(err,data)=>{
                if(err){
                    res.send('内容页查询').end();
                }else {
                    var views=data[0].views;
                    views++;
                    db.query(`UPDATE contents SET views=${views} WHERE id=${id}`,(err)=>{
                        if(err){
                            res.send('浏览数').end();
                        }else {
                            cateid = data[0].categoryid;
                            res.render('web/contents/contents',{
                                userInfo:req.userInfo,
                                navs:navs,
                                cateid:cateid,
                                contents:data[0]
                            });

                        }
                    })


                }
            })
        }
    });
});

router.use('/review',require('../contents/review'));//内容评论页

module.exports = router;